
/*******************************************************************************
 * The code below this comment is integral part of the project "Jelliphy".
 * 
 * "Jelliphy" is a project created by: 
 * Dispage - Patrizio Gelosi
 * Via A. De Gasperi 91 
 * P. Potenza Picena (MC) - Italy
 * 
 * (Hereby referred to as "DISPAGE")
 * 
 * Copyright (c) 2011 DISPAGE.
 * 
 * @Released on: 02-06-2011
 * @Version: 1.0.00
 * @Build: 1109
 * @Site: http://www.dispage.com
 * @Contacts: http://www.dispage.com/contacts
 * 
 * The contents of this file are released under the GNU General Public License
 * version 3 as published by the Free Software Foundation that can be found at
 * the address "http://opensource.org/licenses/gpl-3.0.html". If the license
 * cannot be found, write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * You may not use the present file except in compliance with the License.
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * Dispage" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by Dispage".
 * 
 ******************************************************************************/

JlQ.expr[":"].econtains = function(obj, index, meta, stack){
	return (obj.textContent || obj.innerText || JlQ(obj).text() || "").toLowerCase() == meta[3].toLowerCase();
};

JlQ.autoCloseTags = {'area' : 1, 'base' : 1, 'basefont' : 1, 'br' : 1, 'col' : 1, 'embed' : 1, 'hr' : 1, 'frame' : 1, 'input' : 1, 'img' : 1, 'link' : 1, 'meta' : 1, 'param' : 1};


(function(JlQ) {
	JlQ.extend(JlQ.fn, {

		outerHTML : function(s) {

			if (!this || !this[0] || !this[0].tagName) return '';

			var attr = this[0].attributes,
				tag = this[0].tagName.toLowerCase(),
				html = this.eq(0).html(),
				astr = '', i, a;

			for (i = 0; a = attr[i]; i++) {
				astr += ' ' + a.name + '="' + a.value.replace(/[\"\\]/g, '\\$&') + '"';
			}
			if (html || !JlQ.autoCloseTags[tag]) {
				html += '</' + tag + '>';
			}
			return '<' + tag + astr + '>' + html;
		},

		cssImportant: function (prop, value, revert) {

			var oldCss,
				
				_cssImp = function (p, v) {

					oldCss = oldCss.replace(RegExp('(?:^|;) *'+p+' *:[^;]*'), '');
					if (oldCss.match(/[^;\s]\s*$/)) oldCss += ';';	
					if (typeof(revert) == 'undefined' || !revert) v += ' !important; ';	
					oldCss += (' ' + p + ':' + v);
				},
					
				_getCss = function (el) {

					var oc = JlQ(el).attr('style');

					if (!oc) oc = '';

					return oc;
				};


			if (typeof(value) == 'undefined' && typeof(prop) == 'string') {

				oldCss = _getCss(this);
				
				return oldCss.search(RegExp('(?:^|;) *'+prop+' *:[^;]*?! *important', 'i')) != -1;
			}
			else {
				return this.each( function () {

					oldCss = _getCss(this);

					if (typeof(prop) == 'object') {
						revert = value;
						JlQ.each(prop, _cssImp);
					}
					else {
						_cssImp(prop, value);
					}
					JlQ(this).attr('style', oldCss);
				});
			}
		},

		setTableColumns : function (n) {

			n = parseInt(n);

			return this.each( function () {

				var $this = JlQ(this),
					hasHeader = $this.attr('withheader') == 'true';
				
				$this.children('tbody').children('tr').each(function (h) {

					var $tr = JlQ(this),
						ntd = $tr.children().length,
						tdhtml = (hasHeader && !h) ? '<th></th>' : '<td></td>';
					
					if (ntd > n) {
						$tr.children('*:gt('+(n -1)+')').remove();
					}
					else if (ntd < n) {
						$tr.append(tdhtml.repeat(n- ntd));
					}
				});
			});
		},

		setTableRows : function (n) {

			var nc = parseInt(this.attr('columns')) || 1;

			n = parseInt(n);

			return this.each( function () {

				var $tb = JlQ(this).children('tbody'),
					ntr = $tb.children('tr:not(:has(th))').length,
					tdhtml = '<td></td>';
					
				if (ntr > n) {
					$tb.children('tr:not(:has(th)):gt('+(n -1)+')').remove();
				}
				else if (ntr < n) {
					$tb.append(('<tr>'+tdhtml.repeat(nc)+'</tr>').repeat(n- ntr));
				}
			});
		},

		toggleTableHeader : function (s) {

			if (typeof(s) != 'boolean') s = (s == 'true');

			var nc = parseInt(this.attr('columns')) || 1;

			return this.each( function () {

				var $ths = JlQ('tbody:first-child th', this);

				if (s && !$ths.length) {
					JlQ('tbody:first-child', this).prepend('<tr>' + ('<th></th>'.repeat(nc)) + '</tr>');
				}
				else if (!s && $ths.length) {
					$ths.parent().remove();
				}
			});
		}
	});
})(JlQ);


String.prototype.repeat = function(num) {

	return new Array(isNaN(num)? 1 : ++num).join(this);
}
